package com.zx.kitchen.index.common.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.entity.CateListBean;
import com.zx.kitchen.index.common.fragment.CateListFragment;
import com.zx.kitchen.index.common.fragment.DishListFragment;
import com.zx.kitchen.index.common.utils.Toasts;


/**
 * Created by Administrator on 2017/3/5.
 */

public class NewBusinessInfoActivity extends FragmentActivity {
    private CateListFragment cateListFragment;
    private DishListFragment dishListFragment;
    private TextView tvSelectedNum;
    private Button btnNext;
    private ImageView toolbarLeft;
    private TextView toolbarTitle;
    private String cache_data = "";
    private String ids = "";
    public boolean isPurchase =false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_business_info);
        isPurchase = getIntent().getBooleanExtra("type",false);
        initView();

    }

    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        tvSelectedNum = (TextView) findViewById(R.id.tv_selected_num);
        btnNext = (Button) findViewById(R.id.btn_next);
        if (isPurchase){
            toolbarTitle.setText("新建采购信息");
        }else {
            toolbarTitle.setText("新建营业信息");
        }
        toolbarTitle.setTextColor(getResources().getColor(R.color.blue));
        tvSelectedNum.setText(Html.fromHtml("已选择" + "<font color='#f36867'>" + 0 + "</font>" + "个商品"));

        showLeft();
        showRight();
        initListener();
    }

    private void initListener() {
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ids.length() > 0) {
                    if (ids.contains(",")) {
                        ids = ids.substring(0, ids.length() - 1);
                        startActivity(new Intent(NewBusinessInfoActivity.this, EditInfoActivity.class).putExtra("ids", ids)
                                .putExtra("cache_data", cache_data)
                                .putExtra("isPurchase",isPurchase));
                    }
                } else {
                    Toasts.show(getBaseContext(), "请选择菜品");
                }
            }
        });
        toolbarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        cateListFragment.setOnDataTransmissionListener(new CateListFragment.OnDataTransmissionListener() {
            @Override
            public void dataTransmission(CateListBean cateListBean) {
                dishListFragment.setData(cateListBean);
            }
        });
        dishListFragment.setCountOnListener(new DishListFragment.OnCountListener() {
            @Override
            public void onCount(int count) {
                tvSelectedNum.setText(Html.fromHtml("已选择" + "<font color='#f36867'>" + count + "</font>" + "个商品"));
            }

            @Override
            public void getData(String idss) {
                ids = idss;
            }

        });
        toolbarTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(NewBusinessInfoActivity.this, IndexActivity.class));
            }
        });
    }


    public void showLeft() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (cateListFragment == null) {
            cateListFragment = new CateListFragment();
            transaction.add(R.id.left_frag, cateListFragment);
        } else {
            transaction.show(cateListFragment);
        }
        transaction.commit();
    }

    public void showRight() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (dishListFragment == null) {
            dishListFragment = new DishListFragment();
            transaction.add(R.id.right_frag, dishListFragment);
        } else {
            transaction.show(dishListFragment);
        }
        transaction.commit();
    }

}
